<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head th:replace="~{common::header('编制采购计划')}"></head>
<body>
<div class="container-fluid">
    <div class="row clearfix">
        <div class="col-md-12 column" style="text-align: center">
            <h3>
                编制采购计划
            </h3>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <hr>
        </div>
    </div>

    <form class="form-horizontal" id="ff" method="post">
        <table class="table table-striped table-condensed table-condensed">
            <tr>
                <td class="col-md-6">
                    <div class="col-md-4 column">
                        <label for="cgjhbh" class="control-label">采购计划编号</label>
                    </div>
                    <div class="col-md-8 column">
                        <input readonly type="text" class="form-control" name="stockNum" id="cgjhbh">
                    </div>
                </td>
                <td class="col-md-6">
                    <div class="col-md-4 column">
                        <label for="cgjhmc" class="control-label">采购计划名称</label>
                    </div>
                    <div class="col-md-8 column">
                        <input name="id" type="text" hidden>
                        <input title="采购计划名称"  name="stockName" type="text" class="form-control" id="cgjhmc">
                    </div>
                </td>
            </tr>
            <tr>
                <td class="col-md-6">
                    <div class="col-md-4 column">
                        <label for="gsmc" class="control-label">公司名称</label>
                    </div>
                    <div class="col-md-8 column">
                        <input readonly name="materialCode" value="天啸公司" type="text" class="form-control" id="gsmc">
                    </div>
                </td>
                <td class="col-md-6">
                    <div class="col-md-4 column">
                        <label for="bzr" class="control-label">编制人</label>
                    </div>
                    <div class="col-md-8 column">
                        <input title="编制人" type="text" value="李亮" class="form-control" name="author" id="bzr">
                        <input type="text" hidden value="1" name="authorId">
                    </div>
                </td>
            </tr>
            <tr>
                <td class="col-md-6">
                    <div class="col-md-4 column">
                        <label for="ysje" class="control-label">预算金额(元)</label>
                    </div>
                    <div class="col-md-8 column">
                        <input title="预算金额(元)"  type="number" min="1.00" step="1.00" name="budget" class="form-control" id="ysje">
                    </div>
                </td>
                <td class="col-md-6">
                    <div class="col-md-4 column">
                        <label for="cgbzsj" class="control-label">采购编制时间</label>
                    </div>
                    <div class="col-md-8 column">
                        <input title="采购编制时间" type="datetime-local" th:value="${cgbzsj}"  class="form-control" name="cgbzsj" id="cgbzsj">
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <table class="table table-striped table-condensed table-bordered table-hover">
                        <tr>
                            <td>序号</td>
                            <td>物资编码</td>
                            <td>物资名称</td>
                            <td>数量</td>
                            <td>计量单位</td>
                            <td>交货期</td>
                            <td>协议价格(元)</td>
                            <td>预算总金额(元)</td>
                            <td>计划价(元/不含税)</td>
                        </tr>
                        <tr th:each="user,index:${orders}">
                            <td th:text="${index.index+1}"></td>
                            <td th:text="${user.id}" hidden name="orders"></td>
                            <td th:text="${user.materialCode}" id="code"></td>
                            <td th:text="${user.materialName}"></td>
                            <td th:text="${user.amount}"></td>
                            <td th:text="${user.measureUnit}"></td>
                            <td th:text="${user.endDate}"></td>
                            <td></td>
                            <td th:text="${user.sumPrice}" name="sum"></td>
                            <td></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td class="col-md-6" >
                    <div class="col-md-4 column">
                        <label for="cgfs" class="control-label" style="color: red">采购方式</label>
                    </div>
                    <div class="col-md-8 column">
                        <select class="form-control" name="stockType" id="cgfs">
                            <option value="C000-1">公开求购</option>
                            <option value="C000-2">定向询价</option>
                        </select>
                    </div>
                </td>
                <td class="col-md-6">

                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <table style="overflow: hidden" id="dg1" class="easyui-datagrid"
                           data-options="
									method:'get',
                                    width:'100%',
                                    onCheck:function(index,row){
                                                    $('#cgfs').val('C000-2');
                                                  },">
                        <thead>
                        <tr>
                            <th data-options="field:'ck',checkbox: true"></th>
                            <th data-options="field:'id'" hidden>Item ID(15%)</th>
                            <th data-options="field:'certificate',width:'8%'">供应商证书编号</th>
                            <th data-options="field:'company',width:'8%'">公司名称</th>
                            <th data-options="field:'kind',width:'8%'">公司性质</th>
                            <th data-options="field:'contact',width:'8%'">联系人</th>
                            <th data-options="field:'phone',width:'8%'">电话</th>
                            <th data-options="field:'fax',width:'8%'">传真</th>
                            <th data-options="field:'email',width:'8%'">电子邮件</th>
                            <th data-options="field:'address',width:'8%'">地址</th>
                            <th data-options="field:'zip',width:'8%'">邮编</th>
                            <th data-options="field:'regFund',width:'8%'">注册资金</th>
                            <th data-options="field:'corporation',width:'8%'">法人代表</th>
                            <th data-options="field:'achievement',width:'8%'">业绩</th>
                        </tr>
                        </thead>
                    </table>
                </td>
            </tr>
            <tr>
                <td class="col-md-6" colspan="2">
                    <div class="col-md-2 column">
                        <label for="bz" class="control-label">备注</label>
                    </div>
                    <div class="col-md-10 column">
                        <textarea class="form-control" id="bz" name="remark" style="resize:none" rows="5"></textarea>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: right">
                    <button type="button" class="btn btn-primary btn-sm" onclick="btn1()">保存</button>
                    <button type="button" class="btn btn-primary btn-sm" onclick="history.back()">返回</button>
                </td>
            </tr>
        </table>
    </form>






<script>

    //算总价
    var sum=0;
    $("td[name=sum]").each(function (){
        sum+=Number($(this).text());
    })
    $("#ysje").val(sum);


    //查找供应商
    $('#dg1').datagrid({
        url: '/supplier/findbyMATERIAL_NUM?sb=' + $("#code").text(),
    })

    //不为空时去除 提示 样式
    $("form input[title]").each(function (){
        $(this).on('change',function (){
            $(this).removeAttr("style")
        })
    })

    //提交
    var btn1=function (){
        //判断采购方式
       var selected1= $('#cgfs').val();
       var pd=true;
       var rows=$('#dg1').datagrid("getSelections");
       if (selected1=='C000-2'){
           if(rows.length==0){
               pd=false;
               alert("定向询价要选择供应商哦");
           }else{
               //如果符合要求 则吧选中的供应商 id 添加到 input中
               var temp=$('#ff');
               for (var i = 0; i < rows.length; i++) {
                   var opt = document.createElement("input");
                   opt.hidden='hidden';
                   opt.name = "nums";
                   opt.value =rows[i].id;
                   temp.append(opt);
               }
           }
       }else{
           if(rows.length!=0){
               pd=false;
               alert("公开询价不能选择供应商哦");
           }
       }



       //判断必填项
       $("input[title]").each(function (){
           if ($(this).val()==""){
               $(this).attr("style","border:1px solid red");
               pd=false;
           }
       })

        //提交
       if (pd){

           //将需求计划 id添加到表单
           $('td[name=orders]').each(function (){
               var temp=$('#ff');
               var opt = document.createElement("input");
               opt.name = "orders";
               opt.value =$(this).text();
               opt.hidden='hidden';
               temp.append(opt);
           })


           $('#ff').form('submit', {
               url:"/stock/insertStock",
               success:function (data){
                   $('input[name=orders]').remove();
                   $('input[name=nums]').remove();
                   if (data){
                       location.href='/planman/Order_ytb_list.html';
                   }
               },

           })
       }
    }

</script>





</div>
</body>
</html>